package com.jsmile.cloud.taskcenter.task.kit;

import java.util.Date;

import org.quartz.impl.triggers.CronTriggerImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CronKit {

    private static final Logger logger = LoggerFactory.getLogger(CronKit.class);

    /**
     * 判断cron下一次执行时间是否大于date时间
     *
     * @param cronExpression
     *            表达式
     */
    public static boolean isCronPeriod(final String cronExpression, final Date date) {
        CronTriggerImpl trigger = new CronTriggerImpl();
        try {
            trigger.setCronExpression(cronExpression);
            Date now = trigger.computeFirstFireTime(null);
            return now != null && now.after(date);
        } catch (Exception e) {
            logger.error("【不合法的cron表达式】：{}", e);
        }
        return false;
    }

    /**
     * 判断cron下一次执行时间是否大于当前时间
     *
     * @param cron
     *            表达式
     */
    public static boolean isCronPeriod(final String cron) {
        return isCronPeriod(cron, new Date());
    }
}
